﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Runtime.Serialization;
using System;
using System.Xml.Serialization;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC
{
    public class SettingsCommand : Command, ICommandInfo
    {
        public override string HelpText { get { return "Change or view game and character settings"; } }
        
        [NonSerialized, XmlIgnore]
        public static List<string> Words = new List<string>() { "SETTING", "SETTINGS" };

        public override List<string> FirstWords { get { return Words; } }

        [MethodVisible]
        public static List<PerceivedEventAndAudience> PerformMethod(CommandToPerform cmd, IGameThing source, IGameThing target, PropertyData args, PropertyData modifyingArguments)
        {
            List<PerceivedEventAndAudience> events = new List<PerceivedEventAndAudience>();
            GameChar sourceChar = source as GameChar;
            string allParams = (cmd.UserInput == null) ? string.Empty : cmd.UserInput;
            string[] strings = allParams.Split(" ".ToCharArray(), 2, StringSplitOptions.None);

            if (strings.Length < 2)
                return events;

            string setting = strings[0];
            string value = strings[1];

            sourceChar.Settings.AddProperty(setting, value, sourceChar);

            EventToPerceive evt = CreateObservable(source, setting, value);
            events.Add(new PerceivedEventAndAudience() { Event = evt, Audience = new AssociatedIDCollection(sourceChar.AssociatedID) });

            return events;
        }

        [MethodVisible]
        public static EventToPerceive CreateObservable(IAssociatedID source, string setting, string value)
        {
            return new GeneralEventToPerceive()
            {
                ShowToSource = new Event(string.Format("You set setting '{0}' to '{1}'", setting, value), ViewType.None),
                Sources = new AssociatedIDCollection(source.AssociatedID)
            };
        }
    }
}
